package com.zjw.socket.demo.starter.messageService;

import com.zjw.socket.demo.starter.messageService.Message;
import io.vertx.core.json.impl.JsonUtil;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * @author ：zhong.jw
 * @date ：Created in 2024/2/6 14:43
 */
public class MessagePayloadUtils {

    public static void setString(Message message, String str) {
        final byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        message.setBytes(bytes);
    }

    public static String getString(Message message) {
        final byte[] bytes = message.getBytes();
        return new String(bytes, StandardCharsets.UTF_8);
    }


    public static void setLong(Message message, long value) {
        final ByteBuffer byteBuffer = ByteBuffer.allocate(8).putLong(value);
        byteBuffer.flip();
        final byte[] array = byteBuffer.array();
        message.setBytes(array);
    }


    public static long getLong(Message message) {
        return ByteBuffer.wrap(message.getBytes()).getLong();
    }


    public static void setInt(Message message, int value) {
        final ByteBuffer byteBuffer = ByteBuffer.allocate(4).putInt(value);
        byteBuffer.flip();
        final byte[] array = byteBuffer.array();
        message.setBytes(array);
    }

    public static int getInt(Message message) {
        return ByteBuffer.wrap(message.getBytes()).getInt();
    }

//    public static void setObject(Message message, Object object) {
//
//    }
//
//    public static void getObject(Message message, Object object) {
//
//    }
}
